package com.gc.server.controller.api;

import com.gc.server.entity.*;
import com.gc.server.service.*;
import com.gc.server.vo.ImgOrderVo;
import com.yf.annotation.LoginUserId;
import com.yf.response.R;
import com.yf.response.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/order")
public class ApiOrderController {

    @Autowired
    private ImgOrderService imgOrderService;

    @RequestMapping("/type")
    public R type(@LoginUserId Long userId) {
        List<PointTypeEntity> pointTypeEntities = typeService.find(null);
        pointTypeEntities = pointTypeEntities.stream().filter(pointTypeEntity -> pointTypeEntity.getId() != 1).collect(Collectors.toList());
        return R.ok(pointTypeEntities);
    }

    /**
     * 查找总部产品
     * @param uid
     * @return
     */
    @RequestMapping("/goods")
    public R goods(@LoginUserId Long uid) {
        UserEntity userEntity = userService.get(uid);
        List<LinkGoodsEntity> goodsEntities = goodsService.find(null).stream().filter(item->{
            LinkHeadEntity head = headService.get(item.getHeadId());
            if(head==null||userEntity.getLinkName()==null){
                return false;
            }
            return head.getName().equals(userEntity.getLinkName().trim());
        }).collect(Collectors.toList());
        Map<Long, LinkGoodsEntity> map = goodsEntities.stream().collect(Collectors.toMap(LinkGoodsEntity::getId, goods -> goods));
        return R.ok(goodsEntities).putExtra("map", map);
    }

    @Autowired
    private LinkHeadService headService;

    @Autowired
    private UserService userService;

    @Autowired
    private LinkGoodsService goodsService;

    @Autowired
    private PointTypeService typeService;

    @PostMapping("/img")
    public R save(@LoginUserId Long userId, String img, String remark, BigDecimal point, BigDecimal money, Long pointType,Long[] ids,Integer[] numbers) {
        //如果是晒单
        ImgOrderEntity imgOrderEntity = imgOrderService.commit(userId, img, null, null, remark, point, money, pointType,ids,numbers);
        return R.ok(imgOrderEntity);
    }

    //绑定用户
    @PostMapping("/bind")
    public R bind(@LoginUserId Long userId,  String mobile, Long orderId) {
        //如果是晒单
       ImgOrderEntity orderEntity = imgOrderService.bind(userId,mobile,orderId);
        return R.ok(orderEntity);
    }


    @GetMapping("/img")
    public R list(@LoginUserId Long userId, Integer self) {
        List<ImgOrderVo> imgOrderVos;
        if (self == null) { //审核
            imgOrderVos = imgOrderService.listByTop(userId);
        } else {
            imgOrderVos = imgOrderService.listByUserId(userId);
        }
        return R.ok(imgOrderVos);
    }

    @PostMapping("/pass")
    public R pass(@LoginUserId Long userId, Long orderId) {
        throw new RRException("没有权限,由公司处理");
//        ImgOrderEntity orderEntity = imgOrderService.pass(userId, orderId);
//        return R.ok(orderEntity);
    }

    @PostMapping("/refuse")
    public R refuse(@LoginUserId Long userId, Long orderId) {
        ImgOrderEntity orderEntity = imgOrderService.refuse(userId, orderId);
        return R.ok(orderEntity);
    }


    @GetMapping("/img/{id}")
    public R info(@PathVariable Long id) {
        ImgOrderEntity imgOrderEntity = imgOrderService.get(id);
        List<ImgOrderItemEntity> items = imgOrderService.findItems(id);
        imgOrderEntity.setItems(items);
        return R.ok(imgOrderEntity);
    }


}
